import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_business_core/business_deport/components/business_deport_style_item.dart';
import 'package:x3_business_core/business_deport/components/business_deport_style_details_header.dart';
import 'package:x3_business_core/business_deport/components/business_deport_style_details_fab_tab.dart';
import 'package:x3_business_core/business_deport/components/business_deport_style_details_Information_tab.dart';

class BusinessDepotStyleDetails extends StatefulWidget {
  static final String sName = 'DepotStyleDetails';

  @override
  _BusinessDepotStyleDetailsState createState() => _BusinessDepotStyleDetailsState();
}

class _BusinessDepotStyleDetailsState extends State<BusinessDepotStyleDetails> {
  BaseFutureController _mainStyleController = BaseFutureController();

  BaseTabBarPage _baseTabBarPage;

  dynamic _arguments;

  @override
  Widget build(BuildContext context) {
    _arguments = BaseRouterUtil.getArguments(context);
    String styleId = BaseUtils.getKeyData(_arguments, 'styleId').toString();
    List<BaseTabConfig> _tabs = [
      BaseTabConfig(text: '基础属性', child: BusinessDepotStyleDetailsInformationTab(styleId: styleId)),
      BaseTabConfig(text: 'FAB', child: BusinessDepotStyleDetailsFABTab(styleId: styleId)),
      // BaseTabConfig(text: '搭配', child: Container()),
      // BaseTabConfig(text: '评论', child: Container()),
    ];
    _baseTabBarPage = BaseTabBarPage(_tabs);
    return BaseScaffoldComponent(
      title: '商品详情',
      child: BaseScreenRefreshComponent(
        request: BaseScreenRequestModel('/bi/report/app/query/query/skc/img/{styleId}', method: BaseHttpMethodEnum.GET, params: _arguments),
        builder: _buildNestedScrollViewWidget,
      ),
    );
  }

  /// 构建可以滚动的视图
  Widget _buildNestedScrollViewWidget(BaseHttpResultModel result) {
    return BaseNestedScrollView(
      headerWidget: _buildHeaderWidget(result),
      pinnedWidget: BaseNestedScrollPinned(child: _buildTabBarWidget()),
      contentWidget: _baseTabBarPage.buildTabView(),
    );
  }

  /// 创建 tab
  Widget _buildTabBarWidget() {
    return BaseCardComponent(
      padding: const EdgeInsets.all(0),
      margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0),
      child: this._baseTabBarPage.buildTabBar(context, color: Colors.transparent),
    );
  }

  Widget _buildHeaderWidget(BaseHttpResultModel result) {
    dynamic arguments = BaseRouterUtil.getArguments(context);
    String colorId = BaseUtils.getKeyData(arguments, 'colorId');
    return Container(
      margin: const EdgeInsets.only(bottom: 10.0),
      child: Column(
        children: [
          BusinessDepotStyleDetailsHeader(
            styles: result.data,
            colorId: colorId,
            itemPress: (String colorId) {
              _mainStyleController.refresh(params: {'colorId': colorId});
            },
          ),
          _buildStyleContent(result),
        ],
      ),
    );
  }

  Widget _buildStyleContent(BaseHttpResultModel result) {
    String colorId = BaseUtils.getKeyData(_arguments, 'colorId').toString();
    String styleId = BaseUtils.getKeyData(_arguments, 'styleId').toString();
    return BaseCardComponent(
      child: BaseScreenRefreshComponent(
        request: BaseScreenRequestModel('/bi/report/app/query/query/skc/stock', params: {'styleId': styleId, 'colorId': colorId}),
        controller: _mainStyleController,
        builder: (BaseHttpResultModel httpResultModel) {
          return Column(
            children: [
              BusinessDepotStyleItem(dataSource: httpResultModel.data),
              _buildStyleContentBottom(httpResultModel),
            ],
          );
        },
      ),
    );
  }

  Widget _buildStyleContentBottom(BaseHttpResultModel httpResultModel) {
    List<dynamic> datas;
    if (httpResultModel == null || httpResultModel.data == null || httpResultModel.data == "") {
      datas = [
        {'description': '销售数量', 'value': 0},
        {'description': '可配库存', 'value': 0},
        {'description': '销售金额', 'value': 0},
        {'description': '续销周数', 'value': 0},
      ];
    } else {
      String saleNums = BaseUtils.getKeyData(httpResultModel.data, 'saleNums').toString();
      String avaStock = BaseUtils.getKeyData(httpResultModel.data, 'avaStock').toString();
      String saleSums = BaseUtils.getKeyData(httpResultModel.data, 'saleSums').toString();
      String saleWeeks = BaseUtils.getKeyData(httpResultModel.data, 'saleWeeks').toString();
      datas = [
        {'description': '销售数量', 'value': saleNums},
        {'description': '可配库存', 'value': avaStock},
        {'description': '销售金额', 'value': saleSums},
        {'description': '续销周数', 'value': saleWeeks},
      ];
    }

    return Container(
      padding: EdgeInsets.only(top: 16.0),
      child: BaseInformationRow(
        list: datas,
        builder: (item) {
          String text = BaseUtils.getKeyData(item, 'description').toString();
          String value = BaseUtils.getKeyData(item, 'value').toString();
          return BaseInformationRowItem(isAnimation: true, text: text, numberText: value);
        },
      ),
    );
  }
}
